/**
 * Created by coder on 13-12-1.
 */
(function (undefined) {

    var QuickCookie = {
        defaultOptions: {
            path : '/',
            domain : '',
            expires : new Date(new Date().getTime()+3600000),
            secure : ''

        },
        getCookie : function (name){
            return (document.cookie.match(new RegExp("(^"+name+"| "+name+")=([^;]*)"))==null)?"":decodeURIComponent(RegExp.$2);
        },
        setCookie : function (name, value, options){
            name = encodeURIComponent(name);
            value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
            options = this.getOptions(options);


            var cookieString = name + '=' + value;
            cookieString += options.path ? ';path=' + options.path : '';
            cookieString += options.domain ? ';domain=' + options.domain : '';
            cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
            cookieString += options.secure ? ';secure' : '';
            document.cookie = cookieString;
        },
        getOptions : function (options) {
            if(options && options.expires)
            {
                var expires = options.expires !== undefined ? options.expires : (this.defaultOptions.expires || '');
                var expiresType = typeof(expires);

                if (expiresType === 'string' && options.expires !== '')
                {
                    options.expires = new Date(expires);
                }
                else if (expiresType === 'number')
                {
                    options.expires = new Date(+new Date + 1000 * 60 * 60 * 24 * expires);
                }
            }

            return {
                path: options && options.path || this.defaultOptions.path,
                domain: options && options.domain || this.defaultOptions.domain,
                expires: options && options.expires || this.defaultOptions.expires,
                secure: options && options.secure !== undefined ?  options.secure : this.defaultOptions.secure
            };
        },
        clearCookie : function ()
        {
            return this.remove(this.getKeys(this.all()));
        },
        removeCookie : function (keys) {
            keys = this.isArray(keys) ? keys : this.toArray(arguments);
            for (var i = 0, l = keys.length; i < l; i++) {
                this.setCookie(keys[i], '', {expires : -1});
            }
        },
        getAll : function () {
            if (document.cookie === '') return {};
            var cookies = document.cookie.split('; '), result = {};

            for (var i = 0, l = cookies.length; i < l; i++) {
                var item = cookies[i].split('=');
                result[decodeURIComponent(item[0])] = decodeURIComponent(item[1]);
            }
            return result;
        },
        getAllJsonString : function()
        {
            var all = this.getAll();
            return JSON.stringify(all);
        },
        getKeys: function (obj) {
            var keys = [],
                key = '';
            for (key in obj) {
                if (obj.hasOwnProperty(key)) keys.push(key);
            }
            return keys;
        },
        isArray: Array.isArray || function (value) {
            return Object.prototype.toString.call(value) === '[object Array]';
        },
        isPlainObject: function (value) {
            return !!value && Object.prototype.toString.call(value) === '[object Object]';
        },
        toArray: function (value) {
            return Array.prototype.slice.call(value);
        },
        test : function()
        {
            window.QuickCookie.setCookie("login_remember", "1");
            alert(window.QuickCookie.getCookie("login_remember"));
            alert(window.QuickCookie.getAll());
            window.QuickCookie.removeCookie("login_remember");
            alert(window.QuickCookie.getAll());
            window.QuickCookie.setCookie("test", "2", {expires: 20,path:"/",domain:"www.aikan.co"});
            alert(document.cookie);
        }
    };
    window.QuickCookie = QuickCookie;
})();